﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoulDoorEngine.Components.Smart2D.Info;
using SoulDoorEngine.Framework.Components;

namespace SoulDoorEngine.Components.Smart2D.Objects
{
    public partial class GraphicObj : Component, I2DComponent
    {
        private static List<GraphicObj> AllList = new List<GraphicObj>();
        private static List<MouseButtonDownInfo> AllMouseLeftButtonDown = new List<MouseButtonDownInfo>();
        private static List<MouseButtonDownInfo> AllMouseRightButtonDown = new List<MouseButtonDownInfo>();
        private static int _TimesUpdate;

        /// <summary>
        /// 总更新次数
        /// </summary>
        private static int TimesUpdate
        {
            get { return _TimesUpdate; }
            set { _TimesUpdate = value; }
        }

        /// <summary>
        /// 对象数目
        /// </summary>
        private static int CountGO
        {
            get
            {
                return AllList.Count;
            }
        }

        /// <summary>
        /// 更新全局
        /// </summary>
        private static void AfterAllUpdate()
        {
            MouseButtonDownInfo leftMinTemp = null;
            float minValueTemp = 1.1f;
            foreach (MouseButtonDownInfo item in AllMouseLeftButtonDown)
            {
                if (item.Party.LayerDepth < minValueTemp)
                {
                    minValueTemp = item.Party.LayerDepth;
                    leftMinTemp = item;
                }
            }
            if (leftMinTemp != null)
            {
                leftMinTemp.Party.RaiseMouseLeftButtonDownEvent(leftMinTemp.Information.Position);
            }
            AllMouseLeftButtonDown.Clear();

            minValueTemp = 1.1f;
            MouseButtonDownInfo rightMinTemp = null;
            foreach (MouseButtonDownInfo item in AllMouseRightButtonDown)
            {
                if (item.Party.LayerDepth < minValueTemp)
                {
                    minValueTemp = item.Party.LayerDepth;
                    rightMinTemp = item;
                }
            }
            if (rightMinTemp != null)
            {
                //2012.02.21将后面的leftMinTemp改为rightMinTemp，重要*****************************
                rightMinTemp.Party.RaiseMouseRightButtonDownEvent(rightMinTemp.Information.Position);
            }
            AllMouseRightButtonDown.Clear();

            //更新计数器清零
            GraphicObj.TimesUpdate = 0;
        }

        /// <summary>
        /// 增加一个对象
        /// </summary>
        /// <param name="go"></param>
        public static void Add(GraphicObj go)
        {
            GraphicObj.AllList.Add(go);
        }

        /// <summary>
        /// 移除一个对象
        /// </summary>
        /// <param name="go"></param>
        public static void Remove(GraphicObj go)
        {
            go.Disable();
            AllList.Remove(go);
            foreach (MouseButtonDownInfo item in AllMouseLeftButtonDown)
            {
                if (item.Party == go)
                {
                    AllMouseLeftButtonDown.Remove(item);
                }
            }
            foreach (MouseButtonDownInfo item in AllMouseRightButtonDown)
            {
                if (item.Party == go)
                {
                    AllMouseRightButtonDown.Remove(item);
                }
            }
            go = null;
        }

        /// <summary>
        /// 清除所有对象
        /// </summary>
        public static void Clear()
        {
            AllList.Clear();
            AllMouseLeftButtonDown.Clear();
            AllMouseRightButtonDown.Clear();
        }

        private static void Plus()
        {
            TimesUpdate++;
        }
    }
}
